<?php

namespace app\admin\command;

use Carbon\Carbon;
use fast\Random;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Env;
use think\Exception;
use think\Log;
use app\admin\model\Area;
use app\admin\model\Tarea;
use Overtrue\Pinyin\Pinyin;

class Test extends Command
{

    // 在CalPersonSalary后运行
    protected function configure()
    {
        $this->setName('test')
            ->setDescription('update depart fee  零点并在结算薪资后运行');
    }
    
    protected function execute(Input $input, Output $output)
    {
        @ini_set("memory_limit",'-1');
        $stime=microtime(true);
        $keys = ['国际机场', '机场'];
        $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
        /* 
        $innerFile = ROOT_PATH . "public/template/inner";
        $innerContent = trim(file_get_contents($innerFile));
        $inners = json_decode($innerContent, true);
        $cityList = $inners['recommendGroupList'][0]['indexedCity']['cityList'];
        foreach ($cityList as $city) {
            $name = $city['name'];
            $code = $city['code'];
            $airports = $city['airportlst'];
            $areaModel = Area::where("shortname", $name)->where('level', '2')->find();
            $pid = 0;
            if ($areaModel) {
                $pid = $areaModel->id;
                $areaModel->area_code = $code;
                $areaModel->save();
                foreach ($airports as $air) {
                    $airCode = $air['code'];
                    $airName = $air['name'];
                    // 没有城市名
                    if (str_replace($name, '', $airName) == $airName) {
                        $airName = $name . $airName;
                    }
                    $airShortname = str_replace($keys, '', $airName);
                    $airModel = new Area();

                    $params['pid']  = $pid;
                    $params['name'] = $airName;
                    $params['airport_3'] = $airCode;
                    $params['shortname'] = $airShortname;
                    $params['in_out'] = '1';
                    $params['area_type'] = '2';
                    $content = $params['shortname'];
                    $py = $pinyin->abbr($content, '');
                    $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
                    $params['pinyin'] = $pinyin->permalink($content, '');
                    $airModel->save($params);
                }
            }
        }
         */
        
        $outerFile = ROOT_PATH . "public/template/outer";
        $outerContent = trim(file_get_contents($outerFile));
        $outers = json_decode($outerContent, true);
        $a3755 = $outers[0];
        $a3753 = $outers[1];
        $a3754 = $outers[2];
        $a3756 = $outers[3];
        $a3752 = $outers[4];
        $n = 0;
        $pid1 = 3755;
        foreach ($a3755 as $city) {
            $cityName = str_replace('市', '', $city[0]);
            $cityCode = $city[3];
            $tarea = Tarea::where('city', $cityName)->find();
            echo $cityName . "**" . $cityCode . PHP_EOL;
            $pid2 = 0;
            if ($tarea) {
                $countryName = $tarea->country;
                $countryCode = $tarea->country_code;
                // 插入国家
                echo $countryName . "--" . $cityName . PHP_EOL;
                // 国家ID
                $area1Model = Area::where('shortname', $countryName)->where('level', '2')->where('in_out', '2')->find();
                if ($area1Model) {
                    $pid2 = $area1Model->id;
                } else {
                    // 
                    $area1Model = new Area();
                    $params['pid']  = $pid1;
                    $params['name'] = $countryName;
                    $params['area_code'] = $countryCode;
                    $params['shortname'] = $countryName;
                    $params['in_out'] = '2';
                    $params['level'] = '2';
                    $params['area_type'] = '1';
                    $content = $params['shortname'];
                    $py = $pinyin->abbr($content, '');
                    $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
                    $params['pinyin'] = $pinyin->permalink($content, '');
                    $area1Model->save($params);
                    $pid2 = $area1Model->id;
                }
            } else {
                $n++;
                /*
                $tarea = Tarea::where('city_code', $cityCode)->find();
                if ($tarea) {
                    $countryName = $tarea->country;
                    $countryCode = $tarea->country_code;
                    echo $countryName . "&&" . $cityName . PHP_EOL;
                }
                 */
            }
            // 插入城市
            $area1Model = new Area();
            $params['pid']  = $pid2;
            $params['name'] = $cityName;
            $params['area_code'] = $cityCode;
            $params['shortname'] = $cityName;
            $params['level'] = '3';
            $params['in_out'] = '2';
            $params['area_type'] = '1';
            $content = $params['shortname'];
            $py = $pinyin->abbr($content, '');
            $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
            $params['pinyin'] = $pinyin->permalink($content, '');
            $area1Model->save($params);
        }
        
        $pid1 = 3753;
        foreach ($a3753 as $city) {
            $cityName = str_replace('市', '', $city[0]);
            $cityCode = $city[3];
            // var_dump($city);
            $tarea = Tarea::where('city', $cityName)->find();
            echo $cityName . "**" . $cityCode . PHP_EOL;
            $pid2 = 0;
            if ($tarea) {
                $countryName = $tarea->country;
                $countryCode = $tarea->country_code;
                echo $countryName . "--" . $cityName . PHP_EOL;
                // 国家ID
                $area1Model = Area::where('shortname', $countryName)->where('level', '2')->where('in_out', '2')->find();
                if ($area1Model) {
                    $pid2 = $area1Model->id;
                } else {
                    // 
                    $area1Model = new Area();
                    $params['pid']  = $pid1;
                    $params['name'] = $countryName;
                    $params['area_code'] = $countryCode;
                    $params['shortname'] = $countryName;
                    $params['level'] = '2';
                    $params['in_out'] = '2';
                    $params['area_type'] = '1';
                    $content = $params['shortname'];
                    $py = $pinyin->abbr($content, '');
                    $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
                    $params['pinyin'] = $pinyin->permalink($content, '');
                    $area1Model->save($params);
                    $pid2 = $area1Model->id;
                }
            } else {
                $n++;
                /*
                $tarea = Tarea::where('city_code', $cityCode)->find();
                if ($tarea) {
                    $countryName = $tarea->country;
                    $countryCode = $tarea->country_code;
                    echo $countryName . "&&" . $cityName . PHP_EOL;
                }
                 */
            }
            // 插入城市
            $area1Model = new Area();
            $params['pid']  = $pid2;
            $params['name'] = $cityName;
            $params['area_code'] = $cityCode;
            $params['shortname'] = $cityName;
            $params['level'] = '3';
            $params['in_out'] = '2';
            $params['area_type'] = '1';
            $content = $params['shortname'];
            $py = $pinyin->abbr($content, '');
            $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
            $params['pinyin'] = $pinyin->permalink($content, '');
            $area1Model->save($params);
        }
        
        $pid1 = 3754; 
        foreach ($a3754 as $city) {
            $cityName = str_replace('市', '', $city[0]);
            $cityCode = $city[3];
            $tarea = Tarea::where('city', $cityName)->find();
            echo $cityName . "**" . $cityCode . PHP_EOL;
            if ($tarea) {
                $countryName = $tarea->country;
                $countryCode = $tarea->country_code;
                echo $countryName . "--" . $cityName . PHP_EOL;
                // 国家ID
                $area1Model = Area::where('shortname', $countryName)->where('level', '2')->where('in_out', '2')->find();
                if ($area1Model) {
                    $pid2 = $area1Model->id;
                } else {
                    // 
                    $area1Model = new Area();
                    $params['pid']  = $pid1;
                    $params['name'] = $countryName;
                    $params['area_code'] = $countryCode;
                    $params['shortname'] = $countryName;
                    $params['level'] = '2';
                    $params['in_out'] = '2';
                    $params['area_type'] = '1';
                    $content = $params['shortname'];
                    $py = $pinyin->abbr($content, '');
                    $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
                    $params['pinyin'] = $pinyin->permalink($content, '');
                    $area1Model->save($params);
                    $pid2 = $area1Model->id;
                }
            } else {
                $n++;
                /*
                $tarea = Tarea::where('city_code', $cityCode)->find();
                if ($tarea) {
                    $countryName = $tarea->country;
                    $countryCode = $tarea->country_code;
                    echo $countryName . "&&" . $cityName . PHP_EOL;
                }
                 */
            }
            // 插入城市
            $area1Model = new Area();
            $params['pid']  = $pid2;
            $params['name'] = $cityName;
            $params['area_code'] = $cityCode;
            $params['shortname'] = $cityName;
            $params['level'] = '3';
            $params['in_out'] = '2';
            $params['area_type'] = '1';
            $content = $params['shortname'];
            $py = $pinyin->abbr($content, '');
            $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
            $params['pinyin'] = $pinyin->permalink($content, '');
            $area1Model->save($params);
        }

        $pid1 = 3756;
        foreach ($a3756 as $city) {
            $cityName = str_replace('市', '', $city[0]);
            $cityCode = $city[3];
            $tarea = Tarea::where('city', $cityName)->find();
            echo $cityName . "**" . $cityCode . PHP_EOL;
            if ($tarea) {
                $countryName = $tarea->country;
                $countryCode = $tarea->country_code;
                echo $countryName . "&&" . $cityName . PHP_EOL;
                // 国家ID
                $area1Model = Area::where('shortname', $countryName)->where('level', '2')->where('in_out', '2')->find();
                if ($area1Model) {
                    $pid2 = $area1Model->id;
                } else {
                    // 
                    $area1Model = new Area();
                    $params['pid']  = $pid1;
                    $params['name'] = $countryName;
                    $params['area_code'] = $countryCode;
                    $params['shortname'] = $countryName;
                    $params['level'] = '2';
                    $params['in_out'] = '2';
                    $params['area_type'] = '1';
                    $content = $params['shortname'];
                    $py = $pinyin->abbr($content, '');
                    $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
                    $params['pinyin'] = $pinyin->permalink($content, '');
                    $area1Model->save($params);
                    $pid2 = $area1Model->id;
                }
            } else {
                $n++;
                /*
                $tarea = Tarea::where('city_code', $cityCode)->find();
                if ($tarea) {
                    $countryName = $tarea->country;
                    $countryCode = $tarea->country_code;
                    echo $countryName . "&&" . $cityName . PHP_EOL;
                }
                 */
            }
            // 插入城市
            $area1Model = new Area();
            $params['pid']  = $pid2;
            $params['name'] = $cityName;
            $params['area_code'] = $cityCode;
            $params['shortname'] = $cityName;
            $params['level'] = '3';
            $params['in_out'] = '2';
            $params['area_type'] = '1';
            $content = $params['shortname'];
            $py = $pinyin->abbr($content, '');
            $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
            $params['pinyin'] = $pinyin->permalink($content, '');
            $area1Model->save($params);
        }

        $pid1 = 3752;
        foreach ($a3752 as $city) {
            $cityName = str_replace('市', '', $city[0]);
            $cityCode = $city[3];
            $tarea = Tarea::where('city', $cityName)->find();
            echo $cityName . "**" . $cityCode . PHP_EOL;
            if ($tarea) {
                $countryName = $tarea->country;
                $countryCode = $tarea->country_code;
                echo $countryName . "--" . $cityName . PHP_EOL;
                // 国家ID
                $area1Model = Area::where('shortname', $countryName)->where('level', '2')->where('in_out', '2')->find();
                if ($area1Model) {
                    $pid2 = $area1Model->id;
                } else {
                    // 
                    $area1Model = new Area();
                    $params['pid']  = $pid1;
                    $params['name'] = $countryName;
                    $params['area_code'] = $countryCode;
                    $params['shortname'] = $countryName;
                    $params['level'] = '2';
                    $params['in_out'] = '2';
                    $params['area_type'] = '1';
                    $content = $params['shortname'];
                    $py = $pinyin->abbr($content, '');
                    $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
                    $params['pinyin'] = $pinyin->permalink($content, '');
                    $area1Model->save($params);
                    $pid2 = $area1Model->id;
                }
            } else {
                $n++;
                /*
                $tarea = Tarea::where('city_code', $cityCode)->find();
                if ($tarea) {
                    $countryName = $tarea->country;
                    $countryCode = $tarea->country_code;
                    echo $countryName . "&&" . $cityName . PHP_EOL;
                }
                 */
            }
            // 插入城市
            $area1Model = new Area();
            $params['pid']  = $pid2;
            $params['name'] = $cityName;
            $params['area_code'] = $cityCode;
            $params['shortname'] = $cityName;
            $params['level'] = '3';
            $params['in_out'] = '2';
            $params['area_type'] = '1';
            $content = $params['shortname'];
            $py = $pinyin->abbr($content, '');
            $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
            $params['pinyin'] = $pinyin->permalink($content, '');
            $area1Model->save($params);
        }
        /*
        $cityList = $outers['result']['groups'];
        foreach ($cityList as $city) {
            $name1 = $city['name'];
            $citys = $city['cities'];
            $area1Model = Area::where("shortname", $name1)->where('level', '1')->where('in_out', '2')->find();
            $pid1 = 0;
            if ($area1Model) {
                $pid1 = $area1Model->id;
                foreach ($citys as $c) {
                    $countryName = $c['countryName'];
                    $country = $c['country'];
                    $pid2 = 0;
                    $area1Model = Area::where('shortname', $countryName)->where('level', '2')->where('in_out', '2')->find();
                    if ($area1Model) {
                        $pid2 = $area1Model->id;
                    } else {
                        $area1Model = new Area();
                        $params2['pid']  = $pid1;
                        $params['name'] = $countryName;
                        $params['airport_3'] = $country;
                        $params['area_code'] = $country;
                        $params['shortname'] = $countryName;
                        $params['in_out'] = '2';
                        $params['area_type'] = '1';
                        $content = $params['shortname'];
                        $py = $pinyin->abbr($content, '');
                        $params['first'] = !empty($py) ? strtoupper(substr($py, 0, 1)) : '';
                        $params['pinyin'] = $pinyin->permalink($content, '');
                        $area1Model->save($params);
                        $pid2 = $area1Model->id;
                    }
                    // 插入机场
                }
            }

        }
         */


        $etime=microtime(true);//获取程序执行结束的时间
        $total=$etime-$stime;   //计算差值
        $output->writeln($total);
        $output->info('update person job success');
    }
    
    
    /**
    * xml转为数组
    *
    * @param mixed $xml xml object
    * @param array $options
    * @access public
    * @return array
    */
    public function xmlToArray($xml, $options = []) {
        $defaults = [
            'namespaceRecursive' => false,  //setting to true will get xml doc namespaces recursively
            'removeNamespace' => false,     //set to true if you want to remove the namespace from resulting keys (recommend setting namespaceSeparator = '' when this is set to true)
            'namespaceSeparator' => ':',    //you may want this to be something other than a colon
            'attributePrefix' => '@',       //to distinguish between attributes and nodes with the same name
            'alwaysArray' => array(),       //array of xml tag names which should always become arrays
            'autoArray' => true,            //only create arrays for tags which appear more than once
            'textContent' => '$',           //key used for the text content of elements
            'autoText' => true,             //skip textContent key if node has no attributes or child nodes
            'keySearch' => false,           //optional search and replace on tag and attribute names
            'keyReplace' => false           //replace values for above search values (as passed to str_replace())
        ];
        $options = array_merge($defaults, $options);
        $namespaces = $xml->getDocNamespaces($options['namespaceRecursive']);
        $namespaces[''] = null; //add base (empty) namespace
     
        //get attributes from all namespaces
        $attributesArray = array();
        foreach ($namespaces as $prefix => $namespace) {
            if ($options['removeNamespace']) $prefix = "";
            foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
                //replace characters in attribute name
                if ($options['keySearch']) $attributeName =
                        str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
                $attributeKey = $options['attributePrefix']
                        . ($prefix ? $prefix . $options['namespaceSeparator'] : '')
                        . $attributeName;
                $attributesArray[$attributeKey] = (string)$attribute;
            }
        }
     
        //get child nodes from all namespaces
        $tagsArray = array();
        foreach ($namespaces as $prefix => $namespace) {
            if ($options['removeNamespace']) $prefix = "";
            foreach ($xml->children($namespace) as $childXml) {
                //recurse into child nodes
                $childArray = self::xmlToArray($childXml, $options);
                // list($childTagName, $childProperties) = each($childArray);
                $childTagName = key($childArray);
                $childProperties = current($childArray);
     
                //replace characters in tag name
                if ($options['keySearch']) $childTagName =
                        str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
                //add namespace prefix, if any
                if ($prefix) $childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
     
                if (!isset($tagsArray[$childTagName])) {
                    //only entry with this key
                    //test if tags of this type should always be arrays, no matter the element count
                    $tagsArray[$childTagName] =
                            in_array($childTagName, $options['alwaysArray']) || !$options['autoArray']
                            ? array($childProperties) : $childProperties;
                } elseif (
                    is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName])
                    === range(0, count($tagsArray[$childTagName]) - 1)
                ) {
                    //key already exists and is integer indexed array
                    $tagsArray[$childTagName][] = $childProperties;
                } else {
                    //key exists so convert to integer indexed array with previous value in position 0
                    $tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
                }
            }
        }
     
        //get text content of node
        $textContentArray = array();
        $plainText = trim((string)$xml);
        if ($plainText !== '') $textContentArray[$options['textContent']] = $plainText;
     
        //stick it all together
        $propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || ($plainText === '')
                ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;
     
        //return node as array
        return [
            $xml->getName() => $propertiesArray
        ];
    }

}
